package com.jinxin.platform.cdzxkacs.controller;

import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdzxkacs.bean.CdzxkacsDevice;
import com.jinxin.platform.cdzxkacs.bean.CdzxkacsUser;
import com.jinxin.platform.cdzxkacs.common.SysUserVO;
import com.jinxin.platform.cdzxkacs.rpc.SysUserRpc;
import com.jinxin.platform.cdzxkacs.service.IAcsDeviceService;
import com.jinxin.platform.cdzxkacs.service.IAcsUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2020-12-26
 * @Author: Lingnan
 **/
@RestController
@RequestMapping("/zxkDevice")
@Slf4j
public class ZxkDeviceController {
    @Autowired
    private IAcsDeviceService acsDeviceService;
    @Autowired
    private SysUserRpc sysUserRpc;
    @Autowired
    private IAcsUserService acsUserService;

    @RequestMapping("/list")
    public Map<String,Object> list(@RequestParam int page, @RequestParam int limit,
                                   @RequestParam(required = false)String name,boolean isFromApp){
        HashMap param = new HashMap();
        param.put("page", page);
        param.put("limit", limit);
        param.put("searchKey", name);
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("code",0);
        if(isFromApp){ // app端需要根据当前登录用户(普通用户)所属的单元查询设备列表
            SysUserVO currentUser = sysUserRpc.getCurrentUser();
            if(currentUser == null){
                resultMap.put("msg","获取当前登录用户信息失败");
                resultMap.put("count", 0);
                resultMap.put("data", null);
                return resultMap;
            }
            // 通过当前登录用户id查询组织的管理员 如果组织管理员的id是当前登录用户就代表带用户是管理员身份
            SysUserVO mgrUser = sysUserRpc.getMgrUserById(currentUser.getUserId());
            boolean isMgr = "admin".equals(currentUser.getUserAccount()) || (mgrUser != null && currentUser.getUserId().equals(mgrUser.getUserId()));
            // int userType = currentUser.getUserType();// 1管理员2普通用户(此方法不行)
            if(!isMgr){
                String cellphoneNo = currentUser.getUserCellphoneNo();
                if(StringUtils.isEmpty(cellphoneNo)){
                    resultMap.put("msg","当前登录用户未绑定手机号");
                    resultMap.put("count", 0);
                    resultMap.put("data", null);
                    return resultMap;
                }
                HashMap param2 = new HashMap();
                param2.put("phone",cellphoneNo);
                List<CdzxkacsUser> userList = acsUserService.list(param2);
                if(userList.size() == 0){
                    resultMap.put("msg","当前用户未授权");
                    resultMap.put("count", 0);
                    resultMap.put("data", null);
                    return resultMap;
                }
                List<String> blockIdList = new ArrayList<>();
                userList.forEach(item->{
                    String blockId = item.getBlockId();
                    if(!StringUtils.isEmpty(blockId) && !blockIdList.contains(blockId)){
                        blockIdList.add(blockId);
                    }
                });
                if(blockIdList.size() > 0){
                    List<CdzxkacsDevice> list = acsDeviceService.list(blockIdList);
                    resultMap.put("msg","");
                    resultMap.put("count", list.size());
                    resultMap.put("data", list);
                }else {
                    resultMap.put("msg","数据有误");
                    resultMap.put("count", 0);
                    resultMap.put("data", null);
                }
                return resultMap;
            }
        }
        resultMap.put("msg","");
        resultMap.put("count", acsDeviceService.listPageCount(param));
        resultMap.put("data", acsDeviceService.listPage(param));
        return resultMap;
    }

    @PostMapping("/saveOrUpdate")
    public JsonResult saveOrUpdate(CdzxkacsDevice sysResourceInfoDO){
        return acsDeviceService.saveOrUpdate(sysResourceInfoDO);
    }

    @GetMapping("/loadCommunityList")
    public JsonResult loadCommunityList(){
        return sysUserRpc.loadCommunityList();
    }

    @GetMapping("/loadBuildingList")
    public JsonResult loadBuildingList(String communityId){
        return sysUserRpc.loadBuildingList(communityId);
    }

    @GetMapping("/delete")
    public JsonResult delete(String id){
        return acsDeviceService.delete(id);
    }

    @GetMapping("/open")
    public JsonResult open(String serialNum,String productCode){
        return sysUserRpc.remoteOpenDoor(serialNum,productCode);
    }

    @PostMapping("/syncData")
    public JsonResult syncDeviceData(){
        return sysUserRpc.syncDeviceData();
    }

    @GetMapping("/listAll")
    public JsonResult listAll(){
        return JsonResult.ok(acsDeviceService.list(new HashMap()));
    }
}
